﻿/****************************************************************
 * Copyright (c) 2010-2013 Besture All Rights Reserved.
 * 
 * File Name:		AdminUserDal 
 * 
 * Created:		2013-6-25 11:49:42
 * Original Author:	nitao 
 * 
 * ------------------------------------------------------------
 * MODIFICATION HISTORY
 * ------------------------------------------------------------
 * When			Who		Why
 * ------------------------------------------------------------
 * 
 *
 ****************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using Besture.Common;
using Besture.Model;


namespace Besture.Dal
{
    public class AdminUserDal : BaseDataAccess
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="loginName">登录帐号</param>
        /// <param name="password">登录密码</param>
        /// <returns></returns>
        public bool Login(string loginName, string password)
        {
            var loginFlag = false;
            try
            {
                StringBuilder selectSql = new StringBuilder();
                selectSql.Append("SELECT * FROM TBAdminUser");
                selectSql.Append(" WHERE");
                selectSql.Append(" LoginName = @LoginName");
                selectSql.Append(" AND Password = @Password");
                DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                base.SetParameter(ldcDBCommand, "@LoginName", DbType.String, loginName);
                base.SetParameter(ldcDBCommand, "@Password", DbType.String, password);

                var adminUserTable = base.GetDataSet(ldcDBCommand).Tables[0];
                //登录成功
                if (adminUserTable.Rows.Count > 0)
                {
                    loginFlag = true;
                }
                return loginFlag;
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        /// <summary>
        /// 用户名是否存在
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns></returns>
        public bool IsExists(string loginName)
        {
            var existsFlag = false;
            try
            {
                StringBuilder selectSql = new StringBuilder();
                selectSql.Append("SELECT * FROM TBAdminUser");
                selectSql.Append(" WHERE");
                selectSql.Append(" LoginName = @LoginName");
                DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                base.SetParameter(ldcDBCommand, "@LoginName", DbType.String, loginName);

                DataTable adminUserTable = base.GetDataSet(ldcDBCommand).Tables[0];
                if (adminUserTable.Rows.Count > 0)
                {
                    existsFlag = true;
                }
                return existsFlag;
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public IList<AdminUserModel> GetModel(int id)
        {
            try
            {
                StringBuilder selectSql = new StringBuilder();
                selectSql.Append("SELECT * FROM TBAdminUser ");
                selectSql.Append("WHERE ID = @ID");
                DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                base.SetParameter(ldcDBCommand, "@ID", DbType.Int32, id);
                DataTable adminUserTable = base.GetDataSet(ldcDBCommand).Tables[0];
                return ModelConvertHelper<AdminUserModel>.ConvertToModelList(adminUserTable); ;
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public IList<AdminUserModel> GetModel(string loginName)
        {
            try
            {
                StringBuilder selectSql = new StringBuilder();
                selectSql.Append("SELECT * FROM TBAdminUser ");
                selectSql.Append("WHERE LoginName = @LoginName");
                DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                base.SetParameter(ldcDBCommand, "@LoginName", DbType.String, loginName);
                DataTable adminUserTable = base.GetDataSet(ldcDBCommand).Tables[0];
                return ModelConvertHelper<AdminUserModel>.ConvertToModelList(adminUserTable); ;
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public DataTable GetDataTable(AdminUserModel model)
        {
            try
            {
                StringBuilder selectSql = new StringBuilder();
                selectSql.Append("SELECT * FROM TBAdminUser ");
                selectSql.Append(" WHERE 1 = 1");
                if (!string.IsNullOrWhiteSpace(model.LoginName))
                {
                    selectSql.Append(" AND LoginName = @LoginName");
                }
                if (!string.IsNullOrWhiteSpace(model.IsFreeze.ToString()))
                {
                    selectSql.Append(" AND IsFreeze = @IsFreeze");
                }

                DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                if (!string.IsNullOrWhiteSpace(model.LoginName))
                {
                    base.SetParameter(ldcDBCommand, "@LoginName", DbType.String, model.LoginName);
                }
                if (!string.IsNullOrWhiteSpace(model.IsFreeze.ToString()))
                {
                    base.SetParameter(ldcDBCommand, "@IsFreeze", DbType.String, model.IsFreeze);
                }

                DataTable adminUserTable = base.GetDataSet(ldcDBCommand).Tables[0];

                return adminUserTable;
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        /// <summary>
        /// 添加管理用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(AdminUserModel model)
        {
            var identityId = -1;
            try
            {
                StringBuilder insertSql = new StringBuilder();
                insertSql.Append("INSERT INTO TBAdminUser");
                insertSql.Append("(");
                insertSql.Append("	LoginName,");
                insertSql.Append("	[Password],");
                insertSql.Append("	IsFreeze,");
                insertSql.Append(" RolesIds,");
                insertSql.Append("	Remarks");
                insertSql.Append(")");
                insertSql.Append("VALUES");
                insertSql.Append("(");
                insertSql.Append("	@LoginName,");
                insertSql.Append(" @Password,");
                insertSql.Append(" @IsFreeze,");
                insertSql.Append(" @RolesIds,");
                insertSql.Append("	@Remarks");
                insertSql.Append(")");
                insertSql.Append("SELECT @@IDENTITY");
                DbCommand ldcDBCommand = base.GetCommand(insertSql.ToString());
                base.SetParameter(ldcDBCommand, "@LoginName", DbType.String, model.LoginName);
                base.SetParameter(ldcDBCommand, "@Password", DbType.String, model.Password);
                base.SetParameter(ldcDBCommand, "@IsFreeze", DbType.Int32, model.IsFreeze);
                base.SetParameter(ldcDBCommand, "@RolesIds", DbType.String, model.RolesIds);
                base.SetParameter(ldcDBCommand, "@Remarks", DbType.String, model.Remarks);
                identityId = base.ExecuteScalarInsert(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return identityId;
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(AdminUserModel model)
        {
            int iTmp = -1;
            try
            {
                StringBuilder updateSql = new StringBuilder();
                updateSql.Append("UPDATE TBAdminUser");
                updateSql.Append(" SET");
                updateSql.Append("	[Password] = @Password,");
                updateSql.Append(" Remarks = @Remarks,");
                updateSql.Append(" RolesIds = @RolesIds,");
                updateSql.Append("	[IsFreeze] = @IsFreeze ");
                updateSql.Append(" WHERE LoginName = @LoginName ");
                DbCommand ldcDBCommand = base.GetCommand(updateSql.ToString());
                base.SetParameter(ldcDBCommand, "@Password", DbType.String, model.Password);
                base.SetParameter(ldcDBCommand, "@Remarks", DbType.String, model.Remarks);
                base.SetParameter(ldcDBCommand, "@RolesIds", DbType.String, model.RolesIds);
                base.SetParameter(ldcDBCommand, "@IsFreeze", DbType.Int32, model.IsFreeze);
                base.SetParameter(ldcDBCommand, "@LoginName", DbType.String, model.LoginName);
                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return iTmp;
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateLastLoginTime(string LoginName)
        {
            int iTmp = -1;
            try
            {
                StringBuilder updateSql = new StringBuilder();
                updateSql.Append("UPDATE TBAdminUser");
                updateSql.Append(" SET");
                updateSql.Append("	[LastLoginTime] = @LastLoginTime ");
                updateSql.Append(" WHERE LoginName = @LoginName ");
                DbCommand ldcDBCommand = base.GetCommand(updateSql.ToString());
                base.SetParameter(ldcDBCommand, "@LastLoginTime", DbType.DateTime,DateTime.Now);
                base.SetParameter(ldcDBCommand, "@LoginName", DbType.String, LoginName);
                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return iTmp;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdatePassword(string LoginName,string password)
        {
            int iTmp = -1;
            try
            {
                StringBuilder updateSql = new StringBuilder();
                updateSql.Append("UPDATE TBAdminUser");
                updateSql.Append(" SET");
                updateSql.Append("	[Password] = @Password ");
                updateSql.Append(" WHERE LoginName = @LoginName ");
                DbCommand ldcDBCommand = base.GetCommand(updateSql.ToString());
                base.SetParameter(ldcDBCommand, "@Password", DbType.String, password);
                base.SetParameter(ldcDBCommand, "@LoginName", DbType.String, LoginName);
                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return iTmp;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            int iTmp = -1;
            try
            {
                StringBuilder updateSql = new StringBuilder();
                updateSql.Append("DELETE FROM TBAdminUser");
                updateSql.Append(" WHERE");
                updateSql.Append("	[ID] = @ID");

                DbCommand ldcDBCommand = base.GetCommand(updateSql.ToString());
                base.SetParameter(ldcDBCommand, "@ID", DbType.Int32, id);
                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return iTmp;
        }

        /// <summary>
        /// 批量删除用户
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public void DeleteList(List<int> idList)
        {
            try
            {
                StringBuilder updateSql = new StringBuilder();
                updateSql.Append("DELETE FROM TBAdminUser");
                updateSql.Append(" WHERE");
                updateSql.Append("	[ID] = @ID");
                foreach (int id in idList)
                {
                    DbCommand ldcDBCommand = base.GetCommand(updateSql.ToString());
                    base.SetParameter(ldcDBCommand, "@ID", DbType.Int32, id);
                    base.ExecuteNonQuery(ldcDBCommand);
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public int DeleteList(string ids)
        {
            int iTmp = -1;
            try
            {
                StringBuilder updateSql = new StringBuilder();
                updateSql.Append("exec('DELETE FROM TBAdminUser");
                updateSql.Append(" WHERE ");
                updateSql.Append(" ID in ('+@ID+') ')");
                DbCommand ldcDBCommand = base.GetCommand(updateSql.ToString());
                base.SetParameter(ldcDBCommand, "@ID", DbType.String, ids);
                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return iTmp;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">password</param>
        /// <returns></returns>
        public int AddUser(string userName,string password)
        {
            var identityId = -1;
            try
            {
                StringBuilder insertSql = new StringBuilder();
                insertSql.Append("INSERT INTO TBAdminUser");
                insertSql.Append("(");
                insertSql.Append("	LoginName,");
                insertSql.Append("	[Password]");
                insertSql.Append(")");
                insertSql.Append("VALUES");
                insertSql.Append("(");
                insertSql.Append("	@LoginName,");
                insertSql.Append(" @Password");
                insertSql.Append(")");
                insertSql.Append("SELECT @@IDENTITY");
                DbCommand ldcDBCommand = base.GetCommand(insertSql.ToString());
                base.SetParameter(ldcDBCommand, "@LoginName", DbType.String, userName);
                base.SetParameter(ldcDBCommand, "@Password", DbType.String, password);
                identityId = base.ExecuteScalarInsert(ldcDBCommand);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return identityId;
        }
    }
}
